//
//  YZPigDocHealthDetailViewModel.m
//  VET
//
//  Created by YZ on 2018/7/30.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZPigDocHealthDetailViewModel.h"
#import "YZPigDocHealthDetailModel.h"
#import "YZPigDocHealthDetailInfoModel.h"
#import "NSDate+Category.h"

@implementation YZPigDocHealthDetailViewModel

@synthesize dataSource = _dataSource;
@synthesize index = _index;

- (instancetype)init
{
    if (self = [super init]) {
        [self config];
    }
    return self;
}

- (void)config
{
    _dataSource = [NSMutableArray array];
}

- (void)headerRefreshSuccess:(void (^)(id))successBlock fail:(void (^)(NSError *))failBlock
{
    YZWeakSelf(self)
    [self reqestImmuneRecordSuccess:^(NSDictionary *obj) {
        [weakself.dataSource removeAllObjects];
        YZPigDocHealthDetailModel *model = [YZPigDocHealthDetailModel mj_objectWithKeyValues:(NSDictionary *)obj[@"data"]];
        YZPigDocHealthDetailInfoModel *objModel = [YZPigDocHealthDetailInfoModel new];
        objModel.title = @"保健对象";
        objModel.content = [NSString stringWithFormat:@"猪只%@",weakself.model.earNumber];
        
        YZPigDocHealthDetailInfoModel *dateModel = [YZPigDocHealthDetailInfoModel new];
        dateModel.title = @"保健时间";
        dateModel.content = [[NSDate dateWithTimeIntervalSince1970:model.careTime.integerValue] localDateStringWithFomaterTpye:@"yy-MM-dd HH:mm"];
        
        YZPigDocHealthDetailInfoModel *typeModel = [YZPigDocHealthDetailInfoModel new];
        typeModel.title = @"保健类型";
        typeModel.content = model.careType.itemValue;
        
        YZPigDocHealthDetailInfoModel *excModel = [YZPigDocHealthDetailInfoModel new];
        excModel.title = @"执行人";
        excModel.content = model.careExecutor;
        
        [weakself.dataSource addObject:@[objModel,dateModel,typeModel,excModel]];
        for (YZPigDocHealthMedicineModel *medModel in model.medicines) {
            
            YZPigDocHealthDetailInfoModel *nameModel = [YZPigDocHealthDetailInfoModel new];
            nameModel.title = @"药物名称";
            nameModel.content = medModel.medicine.itemValue;
            
            YZPigDocHealthDetailInfoModel *numModel = [YZPigDocHealthDetailInfoModel new];
            numModel.title = @"使用剂量";
            numModel.content = [NSString stringWithFormat:@"%@ %@",medModel.medicineDosage,medModel.dosagUnit.itemValue];;
            
            YZPigDocHealthDetailInfoModel *functionModel = [YZPigDocHealthDetailInfoModel new];
            functionModel.title = @"使用方法";
            functionModel.content = medModel.medicineUsage.itemValue;
            
            YZPigDocHealthDetailInfoModel *drurationModel = [YZPigDocHealthDetailInfoModel new];
            drurationModel.title = @"使用天数";
            drurationModel.content = medModel.medicineDuration;
            
            [weakself.dataSource addObject:@[nameModel,numModel,functionModel,drurationModel]];
        }
        
        
        YZPigDocHealthDetailInfoModel *remarkModel = [YZPigDocHealthDetailInfoModel new];
        remarkModel.content = model.remark;
         [weakself.dataSource addObject:@[remarkModel]];
        if (successBlock) {
            successBlock(weakself.dataSource);
        }
    } fail:^(NSError *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}

- (void)reqestImmuneRecordSuccess:(YZNetworkCompletionSuccessBlock)successBlock fail:(YZNetworkCompletionFailBlock)failBlock
{
    self.loading = YES;
    NSDictionary *params = @{@"objectType":@"swine"};
    [YZNetworkManager getWithApi:[NSString stringWithFormat:@"/api/care/%ld/%@",self.model.pigId,self.rid] params:params withHeaders:nil success:^(NSDictionary *obj) {
        self.loading = NO;
        if (successBlock) {
            successBlock(obj);
        }
    } fail:^(NSError *error) {
        self.loading = NO;
        if (failBlock) {
            failBlock(error);
        }
    }];
}
@end
